package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppoinmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private StatementServiceImpl statementService;
    @Override
    public void save(Appointment appointment) {
        appointmentMapper.insert(appointment);
    }

    @Override
    public void update(Appointment Appointment) {
        appointmentMapper.updateByPrimaryKey(Appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }




    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            appointmentMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }
    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
//        if (qo.getName()!=null && qo.getName()!="") {
//            qo.setName(qo.getName().substring(0, qo.getName().lastIndexOf(",")));
//        }
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        List<Appointment> appointments = appointmentMapper.selectForList(qo);
        return new TablePageInfo<Appointment>(appointments);
    }

    @Override
    public void arrival(Long id) {
        Appointment appointment = this.get(id);
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())){
            throw new BusinessException("只有预约中的用户才能到店");
        }
        Date date=new Date();
        Integer status=Appointment.STATUS_ARRIVAL;

        appointmentMapper.arrival(id,date,status);


    }

    @Override
    public void cancelOp(Long id) {
        Appointment appointment = this.get(id);
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())){
            throw new BusinessException("只有预约中的用户才能取消预约");
        }
        Integer status=Appointment.STATUS_CANCEL;
        appointmentMapper.cancelOp(id,status);
    }

    @Override
    public Long saveStatement(Long appointmentId) {
        Statement statement2 = statementService.getByAppointmentId(appointmentId);
            if (statement2!=null){
                return statement2.getId();
            }
        Appointment appointment = this.get(appointmentId);
        if (!Appointment.STATUS_ARRIVAL.equals(appointment.getStatus())){
            throw new BusinessException("需要到店后才能生成结算单");
        }
        Statement statement = new Statement();
        statement.setCustomerName(appointment.getCustomerName());
        statement.setCustomerPhone(appointment.getCustomerPhone());
        statement.setActualArrivalTime(appointment.getActualArrivalTime());
        statement.setLicensePlate(appointment.getLicensePlate());
        statement.setCarSeries(appointment.getCarSeries());
        statement.setServiceType(appointment.getServiceType().longValue());
        statement.setAppointmentId(appointmentId);
        statement.setInfo(appointment.getInfo());
        appointment.setStatus(Appointment.STATUS_SETTLE);
        appointmentMapper.updateByPrimaryKey(appointment);
        statementService.save(statement);
        return statement.getId();
    }

}
